package team.southcat.wsyygh.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import team.southcat.wsyygh.common.mapper.CategoryMapper;
import team.southcat.wsyygh.common.service.CategoryService;
import team.southcat.wsyygh.model.enums.CategoryEnum;
import team.southcat.wsyygh.model.pojo.common.Area;
import team.southcat.wsyygh.model.pojo.common.Category;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    //查询所有医院等级
    @Override
    @Cacheable("category_cache_a")
    public List<Category> getAllHospitalType() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, CategoryEnum.getCodeByName("医院等级"));
        List<Category> categories = baseMapper.selectList(queryWrapper);
        return categories;
    }

    //查询所有证件类型
    @Override
    public List<Category> getAllCertificateType() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, CategoryEnum.getCodeByName("证件类型"));
        List<Category> categories = baseMapper.selectList(queryWrapper);
        return categories;
    }

    //查询所有学历
    @Override
    public List<Category> getAllEducationType() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, CategoryEnum.getCodeByName("学历"));
        List<Category> categories = baseMapper.selectList(queryWrapper);
        return categories;
    }

    //查询所有民族
    @Override
    public List<Category> getAllNationalityType() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, CategoryEnum.getCodeByName("民族"));
        List<Category> categories = baseMapper.selectList(queryWrapper);
        return categories;
    }

    @Override
    @Cacheable("category_cache_c")
    public List<Category> getChildByParentId(int parentId) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId,parentId);
        List<Category> categories = baseMapper.selectList(queryWrapper);
        //向list集合每个分类信息中设置hasChildren
        for(Category category:categories){
            int categoryId = category.getId();
            boolean isChild = this.isChildren(categoryId);
            category.setHasChildren(isChild);
        }
        return categories;
    }

    //判断id下面是否有子节点
    private  boolean isChildren(int id){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, id);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count>0;
    }
}
